package handler

import (
	"encoding/json"
	"github.com/gin-gonic/gin"
	"practice-hub/cache"
	"practice-hub/model"
	"practice-hub/pck"
	"strconv"
	"time"
)

func Loginss(c *gin.Context) {
	username := c.PostForm("username")
	password := c.PostForm("password")
	if username == "" || password == "" {
		c.JSON(200, gin.H{"code": 500, "msg": "数据不能为空"})
		return
	}
	redis_key := "sys_error_" + username
	errCout, _ := cache.Rdb.Get(cache.Ctx, redis_key).Int()
	if errCout >= 3 {
		c.JSON(200, gin.H{"code": 500, "msg": "禁止登录"})
		return
	}
	var user model.User
	err := model.DB.Where("username=?", username).Find(&user).Error
	if err != nil {
		c.JSON(200, gin.H{"code": 500, "msg": "查询失败"})
		return
	}
	if user.ID == 0 {
		c.JSON(200, gin.H{"code": 500, "msg": "用户名不存在"})
		return
	}
	if user.Password != pck.Md5(password) {
		cache.Rdb.Incr(cache.Ctx, redis_key)
		if errCout == 2 {
			cache.Rdb.Set(cache.Ctx, redis_key, 3, time.Hour*2)
			c.JSON(500, gin.H{"code": 500, "msg": "密码错误"})
			return
		} else {
			c.JSON(500, gin.H{"code": 500, "msg": "密码错误"})
		}
	}
	c.JSON(200, gin.H{"code": 200, "msg": "登录成功", "data": user})
	return
}
func AddSySRole(c *gin.Context) {
	uid, _ := strconv.Atoi(c.PostForm("uid"))
	rid, _ := strconv.Atoi(c.PostForm("rid"))
	sys := model.UserRole{
		Uid: uid,
		Rid: rid,
	}
	err := model.DB.Create(&sys).Error
	if err != nil {
		c.JSON(200, gin.H{"code": 500, "msg": "添加失败"})
		return
	}
	c.JSON(200, gin.H{"code": 500, "msg": "添加成功"})
	return
}
func AddSysNode(c *gin.Context) {
	nid, _ := strconv.Atoi(c.PostForm("nid"))
	rid, _ := strconv.Atoi(c.PostForm("rid"))
	sys := model.RoleNode{
		Nid: nid,
		Rid: rid,
	}
	err := model.DB.Create(&sys).Error
	if err != nil {
		c.JSON(200, gin.H{"code": 500, "msg": "添加失败"})
		return
	}
	c.JSON(200, gin.H{"code": 200, "msg": "添加成功"})
	return
}
func SysUserRole(c *gin.Context) {
	uid := c.Query("uid")
	var roles []model.Role
	err := model.DB.Table("roles").
		Joins("left join user_roles.rid=roles.id").
		Where("user_roles.uid=?", uid).
		Find(&roles).Error
	if err != nil {
		c.JSON(200, gin.H{"code": 500, "msg": "查询失败"})
		return
	}
	c.JSON(200, gin.H{"code": 200, "msg": "查询成功"})
	return
}
func SysRoleNode(c *gin.Context) {
	rid := c.Query("rid")
	var nodes []model.Node
	err := model.DB.Table("nodes").
		Joins("left join role_nodes.nid=nodes.id").
		Where("role_nodes.rid=?", rid).
		Find(&nodes).Error
	if err != nil {
		c.JSON(200, gin.H{"code": 500, "msg": "查询失败"})
		return
	}
	c.JSON(200, gin.H{"code": 200, "msg": "查询成功"})
	return
}
func AddSysGood(c *gin.Context) {
	good_code := c.PostForm("good_code")
	good_name := c.PostForm("good_name")
	good := model.Good{
		GoodCode: good_code,
		GoodName: good_name,
	}
	err := model.DB.Create(&good).Error
	if err != nil {
		c.JSON(200, gin.H{"code": 500, "msg": "添加失败"})
		return
	}
	c.JSON(200, gin.H{"code": 200, "msg": "添加成功"})
	return
}
func AddSysGoodOrder(c *gin.Context) {
	order_code := c.PostForm("order_code")
	order_num, _ := strconv.Atoi(c.PostForm("order_num"))
	order_price, _ := strconv.Atoi(c.PostForm("order_price"))
	gid, _ := strconv.Atoi(c.PostForm("gid"))
	order := model.GoodOrder{
		OrderCode:  order_code,
		OrderNum:   order_num,
		OrderPrice: order_price,
		Gid:        gid,
	}
	err := model.DB.Create(&order).Error
	if err != nil {
		c.JSON(200, gin.H{"code": 500, "msg": "添加失败"})
		return
	}
	c.JSON(200, gin.H{"code": 200, "msg": "添加成功"})
	return
}
func SysGoodOrderList(c *gin.Context) {
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	size, _ := strconv.Atoi(c.DefaultQuery("size", "5"))
	offset := (page - 1) * size
	var good []model.GoodOrderList
	redis_key := "good_page" + strconv.Itoa(page)
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		model.DB.Table("good_orders").
			Select("good_orders.id AS OrderId",
				"order_code AS OrderCode",
				"order_num AS OrderNum",
				"order_price AS OrderPrice",
				"good_code AS GoodCode",
				"good_name AS GoodName").
			Joins("LEFT JOIN goods ON goods.id = good_orders.gid").
			Offset(offset).Limit(size).Debug().Find(&good)
		jsonData, _ := json.Marshal(good)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &good)
	}
	var total int64
	model.DB.Model(&model.GoodOrder{}).Count(&total)
	c.JSON(200, gin.H{"code": 200, "msg": "查询成功", "data": good, "total": total})
	return
}
